package com.chinawlx.wlxteacher.base;

import android.app.ActivityManager;
import android.content.Context;

import com.chinawlx.wlxteacher.utils.WLXConstant;
import com.chinawlx.wlxteacher.utils.WLXGreenDaoUtil;
import com.chinawlx.wlxteacher.utils.WLXLogUtil;
import com.chinawlx.wlxteacher.wlx_auth;
import com.chinawlx.wlxteacher.wlx_authDao;
import com.chinawlx.wlxteacher.wlx_user;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import okhttp3.Cookie;

/**
 * Created by 吴名 on 16/7/30.
 */
public class WLXApplication extends android.app.Application {

    private static final String TAG = WLXApplication.class.getSimpleName();
    public static WLXApplication myApplication;
    public static wlx_user mActiveUser;//活跃用户

    @Override
    public void onCreate() {
        super.onCreate();
        myApplication = this;
        init();
    }


    /**
     * 初始化账号
     */
    private void init() {
        wlx_authDao authDao = WLXGreenDaoUtil.getAuthDao();
        List<wlx_auth> list = authDao.queryBuilder().list();
        for (wlx_auth auth : list) {
            if (auth.getIs_active() == 1) {
                WLXLogUtil.printE("auth查询到的指定id记录信息:" + auth.toString());
                WLXConstant.USER_ID = auth.getUser_id();
                WLXConstant.IS_ACTIVE = 1;
                WLXConstant.cookieStore = new Gson().fromJson(auth.getCookie(), new
                        TypeToken<List<Cookie>>() {
                }.getType());
                break;
            }
        }
        //找到活跃用户
         mActiveUser = WLXGreenDaoUtil.getUserDao().load(WLXConstant.USER_ID);
    }

    /**
     * 获得当前进程的名字
     *
     * @param context
     * @return 进程号
     */
    public static String getCurProcessName(Context context) {

        int pid = android.os.Process.myPid();

        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context
                .ACTIVITY_SERVICE);

        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
                .getRunningAppProcesses()) {

            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }

}
